package com.example.asus.picturescroll;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private XBanner banner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        dataView();
    }

    private void dataView() {
        int width = LinearLayout.LayoutParams.MATCH_PARENT;
        int height = LinearLayout.LayoutParams.MATCH_PARENT;
        // 添加布局
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width,height);
        banner.setLayoutParams(layoutParams);

        //点击事件
        banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                Toast.makeText(MainActivity.this, "点击了第" + (position + 1) + "图片", Toast.LENGTH_SHORT).show();
            }
        });
        //加载图片
        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
//               加载本地图片展示
                ((ImageView) view).setImageResource((Integer) model);
            }

        });
        ArrayList<Integer> list = new ArrayList<>();
        list.add(R.mipmap.item1);
        list.add(R.mipmap.item2);
        list.add(R.mipmap.item3);
        list.add(R.mipmap.item4);
        list.add(R.mipmap.item5);
        list.add(R.mipmap.item6);
        list.add(R.mipmap.item7);
        list.add(R.mipmap.item8);
        list.add(R.mipmap.item9);
        list.add(R.mipmap.item10);
        banner.setData(list, null);
        banner.setAutoPlayAble(true); //是否支持自动滚动
    }

    private void initView() {
        banner = findViewById(R.id.xbanner);
    }

    /**
     * 为了更好的体验效果建议在下面两个生命周期中调用下面的方法
     **/
    @Override
    protected void onResume() {
        super.onResume();
        banner.startAutoPlay();
    }

    @Override
    protected void onStop() {
        super.onStop();
        banner.stopAutoPlay();
    }

}
